﻿using J.StatusModel;
using J.StatusModel.Dto;
using J.StatusRepository.Interface;
using J.StatusRepository.Migrations;
using StatusDto;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace StatusService
{
    public class RoomNumManageService : IRoomNumManageService
    {
        //房号
        private readonly IRoomNumManageRepository roomNumManageRepository;
        //房型
        private readonly IRoomTypeRepository roomTypeRepository;
        public RoomNumManageService(IRoomNumManageRepository roomNumManageRepository, IRoomTypeRepository roomTypeRepository)
        {
            this.roomNumManageRepository = roomNumManageRepository;
            this.roomTypeRepository = roomTypeRepository;
        }
        /// <summary>
        /// 房号添加
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public async Task<int> RoomNumAdd(RoomNumManage info)
        {
            var list = await roomNumManageRepository.GetAllAsync();
            if (list.Any(item => item.RoomNumber == info.RoomNumber))
            {
                return -1; // 房号重复
            }
            if (list.Any(item => item.Sort == info.Sort))
            {
                return -2; // 排序重复
            }
            info.RoomState = RoomState.空房;
            info.RoomStates = RoomStates.净房;
            //添加房号
            return await roomNumManageRepository.AddAsync(info);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<int> RoomNumDelete(int Id)
        {
            return await roomNumManageRepository.DeleteAsync(Id);
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public async Task<int> RoomNumDeleteAll(string Ids)
        {
            var Id = Ids.Split(',');
            var list = await roomNumManageRepository.GetAllAsync();

            int deletedCount = 0;

            foreach (var item in list)
            {
                if (Id.Contains(item.ID.ToString()))
                {
                    // 调用删除房间号的方法，例如：
                    await roomNumManageRepository.DeleteAsync(item.ID);
                    deletedCount++;
                }
            }

            return deletedCount;
        }
        /// <summary>
        /// 回显
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<RoomNumManage> RoomNumFill(int Id)
        {
            return await roomNumManageRepository.GetModelAsync(Id);
        }
        /// <summary>
        /// 显示分页
        /// </summary>
        /// <param name="TypeId"></param>
        /// <returns></returns>
        public async Task<PageDto<RoomNumDto>> RoomNumShow(RoomStateDto roomStateDto)
        {
            var RoomNum = await roomNumManageRepository.GetAllAsync();
            var RoomType = await roomTypeRepository.GetAllAsync();
            var list = from a in RoomNum
                       join b in RoomType on a.RoomId equals b.ID
                       select new RoomNumDto
                       {
                           ID=a.ID,
                           RoomId=b.ID,
                           RoomTypeName=b.RoomTypeName,
                           RoomNumber=a.RoomNumber,
                           Sort=a.Sort,
                           Comment=a.Comment,
                           IsShelf=a.IsShelf,
                           RoomState = a.RoomState,
                           RoomStates = a.RoomStates,

                       };
            if (roomStateDto.TypeId!=0)
            {
                list = list.Where(x => x.IsShelf == roomStateDto.TypeId);
            }
            if (roomStateDto.RoomId != 0)
            {
                list = list.Where(x => x.RoomId == roomStateDto.RoomId);
            }
            if (!string.IsNullOrEmpty(roomStateDto.RoomNum))
            {
                list = list.Where(x => roomStateDto.RoomNum.Contains(x.RoomNumber.ToString()));
            }
            int PageTotal = list.Count();
            int PageCount = (int)Math.Ceiling(list.Count() * 1.0 / roomStateDto.pageSize);
            list = list.OrderByDescending(x => x.Sort).Skip((roomStateDto.pageIndex - 1) * roomStateDto.pageSize).Take(roomStateDto.pageSize).ToList();
            return new PageDto<RoomNumDto>
            {
                Data = list.ToList(),
                pagedCount = PageCount,
                pageIndex = roomStateDto.pageIndex,
                pageSize = roomStateDto.pageSize,
                totalCount = PageTotal
            };
        }
        /// <summary>
        /// 房态显示
        /// </summary>
        /// <param name="roomStatusDto"></param>
        /// <returns></returns>
        public async Task<PageDto<RoomNumDto>> RoomStatusShow2(RoomStatusDto roomStatusDto)
        {
            var RoomNum = await roomNumManageRepository.GetAllAsync();
            var RoomType = await roomTypeRepository.GetAllAsync();
            var list = from a in RoomNum
                       join b in RoomType on a.RoomId equals b.ID
                       select new RoomNumDto
                       {
                           ID = a.ID,
                           RoomId = b.ID,
                           RoomTypeName = b.RoomTypeName,
                           RoomNumber = a.RoomNumber,
                           Sort = a.Sort,
                           Comment = a.Comment,
                           IsShelf = a.IsShelf,
                           RoomState = a.RoomState,
                           RoomStates = a.RoomStates
                       };
            if (roomStatusDto.roomTypeId != 0)
            {
                list = list.Where(x => x.RoomId == roomStatusDto.roomTypeId);
            }
            if (roomStatusDto.roomStateId != 0)
            {
                list = list.Where(x => x.RoomState == roomStatusDto.roomStateId);
            }
            if (roomStatusDto.moreId != 0)
            {
                list = list.Where(x => x.RoomStates == roomStatusDto.moreId);
            }
            int PageTotal = list.Count();
            int PageCount = (int)Math.Ceiling(list.Count() * 1.0 / roomStatusDto.pageSize);
            list = list.OrderByDescending(x => x.Sort).Skip((roomStatusDto.pageIndex - 1) * roomStatusDto.pageSize).Take(roomStatusDto.pageSize).ToList();
            return new PageDto<RoomNumDto>
            {
                Data = list.ToList(),
                pagedCount = PageCount,
                pageIndex = roomStatusDto.pageIndex,
                pageSize = roomStatusDto.pageSize,
                totalCount = PageTotal
            };
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public async Task<int> RoomNumUpdate(RoomNumManage info)
        {
            return await roomNumManageRepository.UpdateAsync(info);
        }

        /// <summary>
        /// 批量下架
        /// </summary>
        /// <param name="States"></param>
        /// <returns></returns>
        public async Task<int> RoomNumUpdateAll(string Ids, int States)
        {
            var Id = Ids.Split(',');
            var list = await roomNumManageRepository.GetAllAsync();

            int updateCount = 0;
            foreach (var item in list.ToList())
            {
                if (Id.Contains(item.ID.ToString()))
                {
                    item.IsShelf = States;
                    await roomNumManageRepository.UpdateAsync(item); // 更新房间状态到数据库
                    updateCount++;
                }
            }
            return updateCount;
        }
        /// <summary>
        /// 某条数据是否下架
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="State"></param>
        /// <returns></returns>
        public async Task<int> RoomNumUpdateS(int Id, int State)
        {
            var list = await roomNumManageRepository.GetModelAsync(Id);
            list.IsShelf = State;
            return await roomNumManageRepository.UpdateAsync(list); // 更新房间状态到数据库
        }

        /// <summary>
        /// 房态查询
        /// </summary>
        /// <param name="RoomTypeId"></param>
        /// <param name="RoomStateId"></param>
        /// <param name="OtherId"></param>
        /// <returns></returns>
        public async Task<List<RoomNumManage>> RoomStateShow(int RoomTypeId, RoomState RoomStateId, RoomStates OtherId)
        {
            var list = await roomNumManageRepository.GetAllAsync();
            if (RoomTypeId != 0)
            {
                list = list.Where(x => x.RoomId == RoomTypeId).ToList();
            }
            if (RoomStateId != 0)
            {
                list = list.Where(x => x.RoomState == RoomStateId).ToList();
            }
            if (OtherId != 0)
            {
                list = list.Where(x => x.RoomStates == OtherId).ToList();
            }
            return list;
        }
    }
}
